package com.cqndt.site.service.back.impl;


import com.alibaba.fastjson.JSONObject;
import com.cqndt.site.dao.*;
import com.cqndt.site.entity.*;
import com.cqndt.site.service.back.TabJournalRecordService;
import com.cqndt.site.util.Result;
import com.cqndt.site.vo.ConditionVo;
import com.cqndt.site.vo.RecordDetailVo;
import javafx.scene.control.Tab;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class TabJournalRecordServiceImpl implements TabJournalRecordService {

    @Autowired
    private TabJournalRecordMapper recordMapper;
    @Autowired
    private TabGuideMapper guideMapper;
    @Autowired
    private TabContryTrendMapper contryTrendMapper;
    @Autowired
    private TabProvinceTrendMapper provinceTrendMapper;
    @Autowired
    private TabIndustryTrendMapper industryTrendMapper;
    @Autowired
    private TabTenderingMapper tenderingMapper;
    @Autowired
    private TabWinbidMapper winbidMapper;
    @Autowired
    private TabCompeteAnalysisMapper competeAnalysisMapper;
    @Autowired
    private TabAttachmentMapper attachmentMapper;

    @Value("${file-ip}")
    private String fileIp;

    @Value("${front-url}")
    private String frontUrl;




    @Override
    public List<TabJournalRecord> listRecords(ConditionVo vo) {
        if(null != vo.getStartTime() && !("").equals(vo.getStartTime())){
            vo.setStartTime(vo.getStartTime()+" 00:00:00");
        }
        if(null != vo.getEndTime() && !("").equals(vo.getEndTime())){
            vo.setEndTime(vo.getEndTime()+" 23:59:59");
        }
        List<TabJournalRecord> list = recordMapper.listRecords(vo);
        for (TabJournalRecord record:list) {
            record.setLinkUrl(frontUrl+ File.separator+"back"+File.separator+record.getLinkUrl());
        }
        return list;
    }

    @Override
    public Result getRecordById(Integer id) {
        Result result = new Result();
        if(null == id){
            return result.failure(403,"id不能为空");
        }
        //获取期刊记录
        TabJournalRecord record = recordMapper.selectByPrimaryKey(id);
        //获取本期导读
        TabGuide tabGuide = guideMapper.selectByRecordId(id);
        //获取国家行业动态
        TabContryTrend tabContryTrend = contryTrendMapper.selectByRecordId(id);
        if(null !=tabContryTrend && null!= tabContryTrend.getImgId() && !("").equals(tabContryTrend.getImgId())){
            //图片路径
            String[] imgIds = tabContryTrend.getImgId().split(",");
            List<String> imgUrl = attachmentMapper.getUrlByIds(imgIds,fileIp);
            tabContryTrend.setImgUrl(imgUrl);
        }
        //获取各省行业动态
        TabProvinceTrend tabProvinceTrend = provinceTrendMapper.selectByRecordId(id);
        //获取行业学会动态
        List<TabIndustryTrend> tabIndustryTrend = industryTrendMapper.selectByRecordId(id);
        //获取招标信息
        TabTendering tabTendering = tenderingMapper.selectByRecordId(id);
        //获取招标信息
        TabWinbid tabWinbid = winbidMapper.selectByRecordId(id);
        //获取竞争者分析
        List<TabCompeteAnalysis> tabCompeteAnalysis = competeAnalysisMapper.selectByRecordId(id);

        RecordDetailVo vo = new RecordDetailVo();
        vo.setRecord(record);
        vo.setTabCompeteAnalysis(tabCompeteAnalysis);
        vo.setTabContryTrend(tabContryTrend);
        vo.setTabGuide(tabGuide);
        vo.setTabIndustryTrend(tabIndustryTrend);
        vo.setTabProvinceTrend(tabProvinceTrend);
        vo.setTabTendering(tabTendering);
        vo.setTabWinbid(tabWinbid);
        result.setData(vo);
        return result;
    }

    @Override
    public Result delRecordById(Integer id) {
        Result result = new Result();
        if(null == id){
            return result.failure(403,"id不能为空");
        }
        //获取期刊记录
        TabJournalRecord record = recordMapper.selectByPrimaryKey(id);
        if(record.getReleaseState()==2){
            return result.failure(403,"已发布期刊不能删除");
        }
        recordMapper.deleteByPrimaryKey(id);
        return result.success("删除成功");
    }

    @Override
    public Result editRecord(String loginAccount) {
        Result result = new Result();
        RecordDetailVo vo = new RecordDetailVo();
        //获取当前用户是否存在未发布的期刊
        TabJournalRecord record = recordMapper.getByAccountAndState(loginAccount);
        //获取当前发布的最大期数
        if(null == record){
            Integer numMax = recordMapper.getMaxPeriodNum(null);
            record = new TabJournalRecord();
            record.setPeriodNum(null == numMax? 1:numMax+1);
        }else{
            //获取本期导读
            TabGuide tabGuide = guideMapper.selectByRecordId(record.getId());
            //获取国家行业动态
            TabContryTrend tabContryTrend = contryTrendMapper.selectByRecordId(record.getId());
            if(null !=tabContryTrend && null!= tabContryTrend.getImgId() && !("").equals(tabContryTrend.getImgId())){
                //图片路径
                String[] imgIds = tabContryTrend.getImgId().split(",");
                List<String> imgUrl = attachmentMapper.getUrlByIds(imgIds,fileIp);
                tabContryTrend.setImgUrl(imgUrl);
            }
            //获取各省行业动态
            TabProvinceTrend tabProvinceTrend = provinceTrendMapper.selectByRecordId(record.getId());
            //获取行业学会动态
            List<TabIndustryTrend> tabIndustryTrend = industryTrendMapper.selectByRecordId(record.getId());
            //获取招标信息
            TabTendering tabTendering = tenderingMapper.selectByRecordId(record.getId());
            //获取招标信息
            TabWinbid tabWinbid = winbidMapper.selectByRecordId(record.getId());
            //获取竞争者分析
            List<TabCompeteAnalysis> tabCompeteAnalysis = competeAnalysisMapper.selectByRecordId(record.getId());

            vo.setTabCompeteAnalysis(tabCompeteAnalysis);
            vo.setTabContryTrend(tabContryTrend);
            vo.setTabGuide(tabGuide);
            vo.setTabIndustryTrend(tabIndustryTrend);
            vo.setTabProvinceTrend(tabProvinceTrend);
            vo.setTabTendering(tabTendering);
            vo.setTabWinbid(tabWinbid);
        }
        vo.setRecord(record);
        result.setData(vo);
        return result;
    }

//    @Override
//    public Result addOrReleaseRecord(String releaseState, String loginAccount, String record, String tabGuide, String tabContryTrend,
//                                     String tabProvinceTrend, String tabIndustryTrend, String tabTendering, String tabWinbid, String tabCompeteAnalysis) {
//        Result result = new Result();
//        Date date = new Date();
//        if(null == releaseState){
//            return result.failure(403,"请输入操作状态");
//        }
//        try {
//            //记录保存
//            TabJournalRecord tabJournalRecord = new TabJournalRecord();
//            if (null != record) {
//                JSONObject recordObject = JSONObject.parseObject(record);
//                tabJournalRecord = JSONObject.toJavaObject(recordObject, TabJournalRecord.class);
//                tabJournalRecord.setCreateTime(date);
//            }
//            tabJournalRecord.setReleaseAccount(loginAccount);
//            tabJournalRecord.setUpTime(date);
//            Integer state = Integer.parseInt(releaseState);
//            tabJournalRecord.setReleaseState(state);
//            //TODO 连接地址
//            tabJournalRecord.setLinkUrl("xxx");
//            //如果是发布，则查询数据库中最大的已发布期数
//            if (state == 2) {
//                Integer periodNum = recordMapper.getMaxPeriodNum(state);
//                tabJournalRecord.setPeriodNum(null == periodNum ? 1 : periodNum + 1);
//            }
//            if (null != tabJournalRecord.getId()) {
//                recordMapper.updateByPrimaryKeySelective(tabJournalRecord);
//            } else {
//                recordMapper.insertSelective(tabJournalRecord);
//            }
//            //删除相应的关联信息
//            guideMapper.deleteByRecordId(tabJournalRecord.getId());
//            contryTrendMapper.deleteByRecordId(tabJournalRecord.getId());
//            provinceTrendMapper.deleteByRecordId(tabJournalRecord.getId());
//            industryTrendMapper.deleteByRecordId(tabJournalRecord.getId());
//            tenderingMapper.deleteByRecordId(tabJournalRecord.getId());
//            winbidMapper.deleteByRecordId(tabJournalRecord.getId());
//            competeAnalysisMapper.deleteByRecordId(tabJournalRecord.getId());
//            //导读保存
//            if (null != tabGuide) {
//                JSONObject guideObject = JSONObject.parseObject(tabGuide);
//                TabGuide tabGuide1 = JSONObject.toJavaObject(guideObject, TabGuide.class);
//                tabGuide1.setRecordId(tabJournalRecord.getId());
//                guideMapper.insertSelective(tabGuide1);
//            }
//
//            //国家行业动态保存
//            if (null != tabContryTrend) {
//                JSONObject countryObject = JSONObject.parseObject(tabContryTrend);
//                TabContryTrend tabContryTrend1 = JSONObject.toJavaObject(countryObject, TabContryTrend.class);
//                tabContryTrend1.setRecordId(tabJournalRecord.getId());
//                contryTrendMapper.insertSelective(tabContryTrend1);
//            }
//            //各省行业动态
//            if (null != tabProvinceTrend) {
//                JSONObject provinceObject = JSONObject.parseObject(tabContryTrend);
//                TabProvinceTrend provinceTrend = JSONObject.toJavaObject(provinceObject, TabProvinceTrend.class);
//                provinceTrend.setRecordId(tabJournalRecord.getId());
//                provinceTrendMapper.insertSelective(provinceTrend);
//            }
//            //行业协会动态
//            if (null != tabIndustryTrend) {
//                JSONArray json = JSONArray.fromObject(tabIndustryTrend);
//                List<TabIndustryTrend> trendList = (List<TabIndustryTrend>) JSONArray.toCollection(json, TabIndustryTrend.class);
//                for (TabIndustryTrend trend : trendList) {
//                    trend.setRecordId(tabJournalRecord.getId());
//                    industryTrendMapper.insertSelective(trend);
//                }
//            }
//            //招标信息
//            if (null != tabTendering) {
//                JSONObject tenderingObject = JSONObject.parseObject(tabTendering);
//                TabTendering tendering = JSONObject.toJavaObject(tenderingObject, TabTendering.class);
//                tendering.setRecordId(tabJournalRecord.getId());
//                tenderingMapper.insertSelective(tendering);
//            }
//            //中标信息
//            if (null != tabWinbid) {
//                JSONObject winBidObject = JSONObject.parseObject(tabWinbid);
//                TabWinbid winbid = JSONObject.toJavaObject(winBidObject, TabWinbid.class);
//                winbid.setRecordId(tabJournalRecord.getId());
//                winbidMapper.insertSelective(winbid);
//            }
//            //本期竞争者分析
//            if (null != tabCompeteAnalysis) {
//                JSONArray json = JSONArray.fromObject(tabCompeteAnalysis);
//                List<TabCompeteAnalysis> analysisList = (List<TabCompeteAnalysis>) JSONArray.toCollection(json, TabCompeteAnalysis.class);
//                for (TabCompeteAnalysis analysis : analysisList) {
//                    analysis.setRecordId(tabJournalRecord.getId());
//                    competeAnalysisMapper.insertSelective(analysis);
//                }
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//            return result.failure(403,"操作失败");
//        }
//        return result;
//    }
@Override
public Result addOrReleaseRecord(String loginAccount, RecordDetailVo vo) {
    Result result = new Result();
    Date date = new Date();
    if(null == vo.getReleaseState()){
        return result.failure(403,"请输入操作状态");
    }
    try {
        //记录保存
        TabJournalRecord tabJournalRecord = new TabJournalRecord();
        if (null != vo.getRecord()) {
            tabJournalRecord = vo.getRecord();
            tabJournalRecord.setCreateTime(date);
        }
        tabJournalRecord.setReleaseAccount(loginAccount);
        tabJournalRecord.setUpTime(date);
        Integer state = vo.getReleaseState();
        tabJournalRecord.setReleaseState(state);

        //如果是发布，则查询数据库中最大的已发布期数
        if (state == 2) {
            Integer periodNum = recordMapper.getMaxPeriodNum(state);
            tabJournalRecord.setPeriodNum(null == periodNum ? 1 : periodNum + 1);
        }
        //TODO 连接地址
        tabJournalRecord.setLinkUrl("?periodNum="+tabJournalRecord.getPeriodNum());
        if (null != tabJournalRecord.getId()) {
            recordMapper.updateByPrimaryKeySelective(tabJournalRecord);
        } else {
            recordMapper.insertSelective(tabJournalRecord);
        }
        //删除相应的关联信息
        guideMapper.deleteByRecordId(tabJournalRecord.getId());
        contryTrendMapper.deleteByRecordId(tabJournalRecord.getId());
        provinceTrendMapper.deleteByRecordId(tabJournalRecord.getId());
        industryTrendMapper.deleteByRecordId(tabJournalRecord.getId());
        tenderingMapper.deleteByRecordId(tabJournalRecord.getId());
        winbidMapper.deleteByRecordId(tabJournalRecord.getId());
        competeAnalysisMapper.deleteByRecordId(tabJournalRecord.getId());
        //导读保存
        if (null != vo.getTabGuide()) {
            TabGuide tabGuide = vo.getTabGuide();
            tabGuide.setRecordId(tabJournalRecord.getId());
            guideMapper.insertSelective(tabGuide);
        }

        //国家行业动态保存
        if (null != vo.getTabContryTrend()) {
            TabContryTrend tabContryTrend = vo.getTabContryTrend();
            tabContryTrend.setRecordId(tabJournalRecord.getId());
            contryTrendMapper.insertSelective(tabContryTrend);
        }
        //各省行业动态
        if (null != vo.getTabProvinceTrend()) {
            TabProvinceTrend provinceTrend = vo.getTabProvinceTrend();
            provinceTrend.setRecordId(tabJournalRecord.getId());
            provinceTrendMapper.insertSelective(provinceTrend);
        }
        //行业协会动态
        if (null != vo.getTabIndustryTrend()) {
            List<TabIndustryTrend> trendList = vo.getTabIndustryTrend();
            for (TabIndustryTrend trend : trendList) {
                trend.setRecordId(tabJournalRecord.getId());
                industryTrendMapper.insertSelective(trend);
            }
        }
        //招标信息
        if (null != vo.getTabTendering()) {
            TabTendering tendering =  vo.getTabTendering();
            tendering.setRecordId(tabJournalRecord.getId());
            tenderingMapper.insertSelective(tendering);
        }
        //中标信息
        if (null != vo.getTabWinbid()) {
            TabWinbid winbid = vo.getTabWinbid();
            winbid.setRecordId(tabJournalRecord.getId());
            winbidMapper.insertSelective(winbid);
        }
        //本期竞争者分析
        if (null != vo.getTabCompeteAnalysis()) {
            List<TabCompeteAnalysis> analysisList = vo.getTabCompeteAnalysis();
            for (TabCompeteAnalysis analysis : analysisList) {
                analysis.setRecordId(tabJournalRecord.getId());
                competeAnalysisMapper.insertSelective(analysis);
            }
        }
    }catch (Exception e){
        e.printStackTrace();
        return result.failure(403,"操作失败");
    }
    return result;
}
}
